package com.ruoyi.system.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.TBed;
import com.ruoyi.system.mapper.TBedMapper;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.TPatientMapper;
import com.ruoyi.system.domain.TPatient;
import com.ruoyi.system.service.ITPatientService;
import com.ruoyi.common.core.text.Convert;

import javax.annotation.Resource;

/**
 * 患者Service业务层处理
 * 
 * @author 杨一迪
 * @date 2024-12-28
 */
@Service
public class TPatientServiceImpl extends ServiceImpl<TPatientMapper,TPatient> implements ITPatientService

{
    @Autowired
    private TPatientMapper tPatientMapper;

    @Resource
    private TBedMapper bedMapper;

    /**
     * 查询患者
     * 
     * @param id 患者主键
     * @return 患者
     */
    @Override
    public TPatient selectTPatientById(Long id)
    {
        return tPatientMapper.selectTPatientById(id);
    }

    /**
     * 查询患者列表
     * 
     * @param tPatient 患者
     * @return 患者
     */
    @Override
    public List<TPatient> selectTPatientList(TPatient tPatient)
    {
        return tPatientMapper.selectTPatientList(tPatient);
    }

    /**
     * 新增患者
     * 
     * @param tPatient 患者
     * @return 结果
     */
    @Override
    public int insertTPatient(TPatient tPatient)
    {
        tPatient.setCreateTime(DateUtils.getNowDate());
        TBed tBed = bedMapper.selectTBedById(tPatient.getBedId());
        tBed.setIsAvailable(1);
        bedMapper.updateTBed(tBed);
        return tPatientMapper.insertTPatient(tPatient);
    }

    /**
     * 修改患者
     * 
     * @param tPatient 患者
     * @return 结果
     */
    @Override
    public int updateTPatient(TPatient tPatient)
    {
        tPatient.setUpdateTime(DateUtils.getNowDate());
//        释放就床位
        TPatient tPatient1 = tPatientMapper.selectTPatientById(tPatient.getId());
        TBed tBed1 = bedMapper.selectTBedById(tPatient1.getBedId());
        tBed1.setIsAvailable(0);
        bedMapper.updateTBed(tBed1);
//        锁住新床位
        TBed tBed = bedMapper.selectTBedById(tPatient.getBedId());
        tBed.setIsAvailable(1);
        bedMapper.updateTBed(tBed);
        return tPatientMapper.updateTPatient(tPatient);
    }

    /**
     * 批量删除患者
     * 
     * @param ids 需要删除的患者主键
     * @return 结果
     */
    @Override
    public int deleteTPatientByIds(String ids)
    {
        return tPatientMapper.deleteTPatientByIds(Convert.toStrArray(ids));
    }

    /**
     * 批量软删除患者
     *
     * @param ids 需要软删除的患者主键
     * @return 结果
     */
    @Override
    public int softDeleteTPatientByIds(String ids)
    {
        String[] strArray = Convert.toStrArray(ids);
        int count = 0;
        for (String s : strArray) {
            TPatient tPatient = tPatientMapper.selectTPatientById(Long.valueOf(s));
            tPatient.setIsDelete(1);
            int i = tPatientMapper.updateTPatient(tPatient);
            count = count + i;
        }
        return count;
    }

    /**
     * 删除患者信息
     * 
     * @param id 患者主键
     * @return 结果
     */
    @Override
    public int deleteTPatientById(Long id)
    {
        return tPatientMapper.deleteTPatientById(id);
    }
}
